package com.jast.business.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jast.business.domain.BigdataYarnKv;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataZookeeperKv;
import com.jast.business.service.IBigdataZookeeperKvService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

/**
 * Zookeeper监控指标最新值Controller
 * 
 * @author Jast
 * @date 2023-02-16
 */
@Controller
@RequestMapping("/business/zookeeper")
public class BigdataZookeeperKvController extends BaseController
{
    private String prefix = "business/zookeeper";

    @Autowired
    private IBigdataZookeeperKvService bigdataZookeeperKvService;

    @RequiresPermissions("business:zookeeper:view")
    @GetMapping()
    public String zookeeper()
    {
        return prefix + "/zookeeper";
    }

    /**
     * 查询Zookeeper监控指标最新值列表
     */
    @RequiresPermissions("business:zookeeper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataZookeeperKv bigdataZookeeperKv)
    {
        startPage();
        List<BigdataZookeeperKv> list = bigdataZookeeperKvService.selectBigdataZookeeperKvList(bigdataZookeeperKv);
        return getDataTable(list);
    }

    /**
     * 导出Zookeeper监控指标最新值列表
     */
    @RequiresPermissions("business:zookeeper:export")
    @Log(title = "Zookeeper监控指标最新值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataZookeeperKv bigdataZookeeperKv)
    {
        List<BigdataZookeeperKv> list = bigdataZookeeperKvService.selectBigdataZookeeperKvList(bigdataZookeeperKv);
        ExcelUtil<BigdataZookeeperKv> util = new ExcelUtil<BigdataZookeeperKv>(BigdataZookeeperKv.class);
        return util.exportExcel(list, "Zookeeper监控指标最新值数据");
    }

    /**
     * 新增Zookeeper监控指标最新值
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存Zookeeper监控指标最新值
     */
    @RequiresPermissions("business:zookeeper:add")
    @Log(title = "Zookeeper监控指标最新值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataZookeeperKv bigdataZookeeperKv)
    {
        return toAjax(bigdataZookeeperKvService.insertBigdataZookeeperKv(bigdataZookeeperKv));
    }


    /**
     * 更新/新增Zookeeper监控指标最新值
     */
    @Log(title = "Zookeeper监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataZookeeperKv bigdataZookeeperKv)
    {
        return toAjax(bigdataZookeeperKvService.upsertBigdataZookeeperKv(bigdataZookeeperKv));
    }

    /**
     * 修改Zookeeper监控指标最新值
     */
    @RequiresPermissions("business:zookeeper:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataZookeeperKv bigdataZookeeperKv = bigdataZookeeperKvService.selectBigdataZookeeperKvById(id);
        mmap.put("bigdataZookeeperKv", bigdataZookeeperKv);
        return prefix + "/edit";
    }

    /**
     * 修改保存Zookeeper监控指标最新值
     */
    @RequiresPermissions("business:zookeeper:edit")
    @Log(title = "Zookeeper监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataZookeeperKv bigdataZookeeperKv)
    {
        return toAjax(bigdataZookeeperKvService.updateBigdataZookeeperKv(bigdataZookeeperKv));
    }

    /**
     * 删除Zookeeper监控指标最新值
     */
    @RequiresPermissions("business:zookeeper:remove")
    @Log(title = "Zookeeper监控指标最新值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataZookeeperKvService.deleteBigdataZookeeperKvByIds(ids));
    }

    @GetMapping("/monitor")
    public String zookeeperMonitor()
    {
        return "bigdata/zookeeper/zookeeper";
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @PostMapping("/getBasicInfo")
    @ResponseBody
    public AjaxResult getBasicInfo(BigdataZookeeperKv bigdataYarnKv )
    {
        JSONObject jsonObject = new JSONObject();
        List<BigdataZookeeperKv> list = bigdataZookeeperKvService.selectBigdataZookeeperKvList(bigdataYarnKv);
        list.forEach(data->{
            if(data.getHtype().equals("a")){
                switch (data.getHkey()){

                    case "zk_watch_count":
                        jsonObject.put("azk_watch_count",data.getHvalue());
                        break;
                    case "zk_num_alive_connections":
                        jsonObject.put("azk_num_alive_connections",data.getHvalue());
                        break;
                    case "zk_znode_count":
                        jsonObject.put("azk_znode_count",data.getHvalue());
                        break;
                    case "zk_avg_latency":
                        jsonObject.put("azk_avg_latency",data.getHvalue());
                        break;
                    case "zk_max_latency":
                        jsonObject.put("azk_max_latency",data.getHvalue());
                        break;
                    case "zk_packets_received":
                        jsonObject.put("azk_packets_received",data.getHvalue());
                        break;
                    case "zk_packets_sent":
                        jsonObject.put("azk_packets_sent",data.getHvalue());
                        break;
                    case "zk_outstanding_requests":
                        jsonObject.put("azk_outstanding_requests",data.getHvalue());
                        break;
                    case "zk_version":
                        jsonObject.put("azk_version",data.getHvalue());
                        break;
                }
            }
        });
        System.out.println("size;"+list.size());
        System.out.println(JSON.toJSONString(success(jsonObject)));
        return success(jsonObject);
    }
}
